import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.MouseEvent;


public class XTable {

    public static Box createTable() throws ClassNotFoundException {



        JTable table = new JTable(new MyTableModel());
        table.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(false);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(false);
        table.setGridColor(Color.BLACK);
        table.setRowHeight(25);

        table.getTableHeader().setDefaultRenderer(new MyDefaultTableCellRenderer());
        table.addMouseListener(new MouseInputAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()==MouseEvent.BUTTON3) {
                    JPopupMenu popup = XPopupMenu.createPopupMenu();
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        scrollPane.setWheelScrollingEnabled(false);
        scrollPane.setForeground(Color.BLACK);
        scrollPane.setOpaque(true);

        Box vbox = Box.createVerticalBox();
        vbox.setBackground(Color.cyan);
        vbox.add(scrollPane);
        JLabel label1 = new JLabel("每页");
        JLabel label2 = new JLabel("条");
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("10");
        comboBox.addItem("50");
        comboBox.addItem("100");

        Box hbox = Box.createHorizontalBox();
        hbox.add(Box.createHorizontalGlue());
        hbox.add(label1);
        hbox.add(Box.createHorizontalStrut(2));
        hbox.add(comboBox);
        hbox.add(Box.createHorizontalStrut(2));
        hbox.add(label2);
        hbox.add(Box.createHorizontalStrut(2));

        hbox.add(new MyButton(ImageIconUtil.createImageIcon("images/fenye-shouye.png")));
        hbox.add(Box.createHorizontalStrut(2));
        hbox.add(new MyButton(ImageIconUtil.createImageIcon("images/jurassic_previous-page.png")));
        hbox.add(Box.createHorizontalStrut(2));
        hbox.add(new MyButton(ImageIconUtil.createImageIcon("images/jurassic_next-page.png")));
        hbox.add(Box.createHorizontalStrut(2));
        hbox.add(new MyButton(ImageIconUtil.createImageIcon("images/fenye-weiye.png")));
        hbox.add(Box.createHorizontalStrut(2));
        JLabel label3 = new JLabel("共12条");
        hbox.add(label3);
        hbox.add(Box.createHorizontalStrut(2));
        JPanel page = new JPanel();
        page.setLayout(new FlowLayout(FlowLayout.RIGHT));
        page.add(hbox);


        vbox.add(page);

        return vbox;
    }

    static class MyButton extends JButton {
        public MyButton(Icon icon) {
            super(icon);
            setBackground(Color.white);
            setForeground(Color.blue);
        }
    }


    static class MyTableModel extends AbstractTableModel {

        private String[] columns = {"结算单编号", "结算单类型", "结算公司", "结算供应商", "币种", "同步ERP状态", "创建方阵营", "协同模式", "审批进度"};
        private String[][] datas = {
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"},
                {"STAPREPAY20230404000004", "预付款申请", "上海甄云信息科技有限公司", "厦门古龙食品有限公司", "CNY", "同步失败", "采购方", "单边协同", "审批中"}};

        @Override
        public int getRowCount() {
            return datas.length;
        }

        @Override
        public int getColumnCount() {
            return columns.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return  datas[rowIndex][columnIndex];
        }


        public String getColumnName(int col)
        {
            return columns[col];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    }

    static class MyDefaultTableCellRenderer extends DefaultTableCellRenderer {
        public MyDefaultTableCellRenderer() {
            setBorder(BorderFactory.createLineBorder(Color.BLUE));
            setSize(100,30);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            cell.setBackground(Color.green);
            Rectangle rectangle = cell.getBounds();

            JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout(FlowLayout.LEFT));
            panel.setSize(rectangle.width,rectangle.height);


            JLabel label = new JLabel(value.toString());
            panel.setBorder(BorderFactory.createEmptyBorder());
            label.setOpaque(true);
            label.setBackground(Color.pink);
            panel.setBackground(Color.pink);
            label.setForeground(Color.white);
            panel.add(label);

            return panel;
        }
    }

}
